package com.linkage.seapeak.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.linkage.jz.bankruptcy.R;
import com.linkage.seapeak.pojo.CrediterUser;
import com.linkage.seapeak.utils.CommonUtils;
import com.seapeak.recyclebundle.BaseHolder;
import com.seapeak.recyclebundle.BaseRecyclerAdapter;

import java.util.List;

import jp.wasabeef.glide.transformations.CropCircleTransformation;

/**
 * 群发人员列表的Item
 */
public class CrediterAdapter extends BaseRecyclerAdapter<CrediterAdapter.ViewHolder> {

    Context context;
    List<CrediterUser> userDataList;
    boolean showSelect;

    /**
     *
     * @param context
     * @param userDataList
     * @param showSelect 是否展示多选项目
     */
    public CrediterAdapter(Context context, List<CrediterUser> userDataList, boolean showSelect) {
        this.context = context;
        this.userDataList = userDataList;
        this.showSelect = showSelect;
    }

    @NonNull
    @Override
    public CrediterAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_creditor, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull CrediterAdapter.ViewHolder viewHolder, int i) {
        super.onBindViewHolder(viewHolder, i);
        CrediterUser item = userDataList.get(i);
        if (item != null) {
            viewHolder.check.setVisibility(showSelect ? View.VISIBLE : View.GONE);
            viewHolder.check.setImageResource(item.isCheck ? R.drawable.complete_green : R.drawable.complete_full);
            Glide.with(context).load(item.getAvatar())
                    .bitmapTransform(new CropCircleTransformation(context))
                    .placeholder(R.drawable.logo)
                    .into(viewHolder.avatar);
            viewHolder.name.setText(item.getCreditorname());
            viewHolder.money.setText("确认金额：" + CommonUtils.resetMoney(item.getConfirmAmount()) + "元");
            if("CREDITOR".equalsIgnoreCase(item.getRole())) {
                viewHolder.tipsGreen.setVisibility(item.getKindTax() != 0 ? View.VISIBLE : View.GONE);
                viewHolder.tipsRed.setVisibility(item.getKindGuarantee() != 0 ? View.VISIBLE : View.GONE);
                viewHolder.tipsYellow.setVisibility(item.getKindOther() != 0 ? View.VISIBLE : View.GONE);
                viewHolder.tipsGrey.setVisibility(item.getKindOrdinary() != 0 ? View.VISIBLE : View.GONE);
            }else{
                viewHolder.tipsGreen.setVisibility(View.GONE);
                viewHolder.tipsRed.setVisibility(View.GONE);
                viewHolder.tipsYellow.setVisibility(View.GONE);
                viewHolder.tipsGrey.setVisibility(View.GONE);
            }

            viewHolder.tv_item_bgaswipe_online.setVisibility(item.getOnlineStatus() == 2 ? View.VISIBLE : View.GONE);
            String roleStr = setRoleName(item.getRole());
            if("".equalsIgnoreCase(roleStr))
            {
                if(item.getCommitteeChairman() == 1)
                {
                    roleStr = "主席";
                }
            }
            viewHolder.roleName.setText(roleStr);
            viewHolder.roleName.setVisibility(TextUtils.isEmpty(roleStr) ? View.GONE : View.VISIBLE);
        }
    }

    /**
     * 设置全选
     */
    public void setAllSelect(boolean bool){
        for (CrediterUser item : userDataList) {
            item.isCheck = bool;
        }
        notifyDataSetChanged();
    }

    /**
     * 设置的角色名称判断
     * @param role
     * @return
     */
    private String setRoleName(String role) {
        if (role == null)
            return "";
        if (role.equals("CONTRIBUTOR"))
            return "出资人";
        else if (role.equals("STAFF"))
            return "职工代表";
        else
            return "";
    }

    @Override
    public int getItemCount() {
        return userDataList.size();
    }

    class ViewHolder extends BaseHolder {

        ImageView check;
        ImageView avatar;
        TextView name, money;

        TextView roleName;
        TextView tipsGreen;
        TextView tipsRed;
        TextView tipsYellow;
        TextView tipsGrey;
        TextView tv_item_bgaswipe_online;

        public ViewHolder(View view) {
            super(view);
            check = view.findViewById(R.id.check);
            avatar = view.findViewById(R.id.avatar);
            name = view.findViewById(R.id.name);
            tv_item_bgaswipe_online = view.findViewById(R.id.tv_item_bgaswipe_online);
            roleName = (TextView) view.findViewById(R.id.role_name);
            tipsGreen = (TextView) view.findViewById(R.id.tips_green);
            tipsRed = (TextView) view.findViewById(R.id.tips_red);
            tipsYellow = (TextView) view.findViewById(R.id.tips_yellow);
            tipsGrey = (TextView) view.findViewById(R.id.tips_grey);
            money = view.findViewById(R.id.money);
        }
    }
}
